package com.example.myanimator.shadowLayer;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.Shader;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import androidx.annotation.Nullable;

import com.example.myanimator.R;

/**
 * @author tgw
 * @date 2021/3/30
 * @describe 点击显示图片 Matrix 缩放利用shadow显示圆形图片
 */
public class TelescopeView extends View {
    private Paint mPaint;
    private Bitmap mBitmap, mBitmapBG;
    private int mDx, mDy;
    private BitmapShader mBitmapShader;

    public TelescopeView(Context context) {
        super(context);
    }

    public TelescopeView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        mPaint = new Paint();
        mBitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.aa);
        mBitmapShader = new BitmapShader(mBitmap, Shader.TileMode.REPEAT,
                Shader.TileMode.REPEAT);
    }

    public TelescopeView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
//        setMeasuredDimension(300,300);
    }

    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (mBitmapBG == null) {
            //创建一个蒙版，
//            mBitmapBG = Bitmap.createBitmap(getWidth(), getHeight(), Bitmap.Config.
//                    ARGB_8888);
//            Canvas canvasbg = new Canvas(mBitmapBG);
//            canvasbg.drawBitmap(mBitmap, null, new Rect(0, 0, getWidth(), getHeight()),
//                    mPaint);
//            canvas.drawColor(Color.YELLOW);
//            canvas.drawText("这是一个蒙版点击该蒙版可查看 图片", 0, mPaint.getFontMetrics().bottom - mPaint.getFontMetrics().top, mPaint);

        }

        //点击蒙层 展示图片
//        if (mDx != -1 && mDy != -1) {

            //矩阵缩放
            Matrix matrix = new Matrix();
            float scalex = (float) 300 / mBitmap.getWidth();
            float scaley = (float) 300 / mBitmap.getHeight();
            matrix.setScale(scalex, scaley);
            mBitmapShader.setLocalMatrix(matrix);
            mPaint.setShader(mBitmapShader);

            canvas.drawCircle(150, 150, 150, mPaint);
//        }

    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {

            case MotionEvent.ACTION_DOWN:

                mDx = (int) event.getX();
                mDy = (int) event.getY();
                postInvalidate();
                return true;
            case MotionEvent.ACTION_MOVE:
                mDx = (int) event.getX();
                mDy = (int) event.getY();
                break;
            case MotionEvent.ACTION_UP:
            case MotionEvent.ACTION_CANCEL:
                mDx = -1;
                mDy = -1;
                break;

        }
        postInvalidate();
        return super.onTouchEvent(event);
    }

}
